package com.jcfriends.main.user;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jcfriends.common.manager.ModelMapManager;
import com.jcfriends.common.manager.SessionManager;
import com.jcfriends.main.user.Service.UserService;
import com.jcfriends.main.user.pojo.UserInfo;

@Controller
public class UserController {

	@Autowired
	private UserService userService;
	
	private String DEFAULT_PATH = "/m/main/login/";
	
	/**
	 * 로그인 페이지로 이동
	 * @return
	 */
	@RequestMapping(method=RequestMethod.GET, value={"/login"})
	public String intiLogin(){
		
		return DEFAULT_PATH + "login";
	}
	
	/**
	 * 로그인 처리 + 세션정보 입력
	 * @param userInfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(method=RequestMethod.POST, value={"/login"})
	public String userLogin(ModelMap map, UserInfo userInfo) throws Exception{
		
		userInfo = userService.getUser(userInfo);

		if(userInfo != null && userInfo.getUserSeq() != 0){
			SessionManager.setUserSession(userInfo);

			map = ModelMapManager.setModelMap(map, "/main/payment", "GET");
			
			return "/m/common/auto_forward";
		}else{
			return "redirect:/login";
		}
		
	}
}
